package com.github.ecbp.user.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.ecbp.common.constant.ConstantCode;
import com.github.ecbp.common.data.service.impl.BaseServiceImpl;
import com.github.ecbp.user.service.entity.TMenu;
import com.github.ecbp.user.service.entity.TRole;
import com.github.ecbp.user.service.entity.TRoleMenuRelation;
import com.github.ecbp.user.service.mapper.RoleMenuRelationMapper;
import com.github.ecbp.user.service.service.RoleMenuRelationService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class RoleMenuRelationServiceImpl extends BaseServiceImpl<RoleMenuRelationMapper, TRoleMenuRelation> implements RoleMenuRelationService {

    @Override
    public List<TMenu> getMenuByRoleId(Long roleId) {
        QueryWrapper<TRoleMenuRelation> menuRelationQueryWrapper = new QueryWrapper<>();
        menuRelationQueryWrapper.select("distinct m.id", "m.parent_id", "m.title", "m.level", "m.sort", "m.name",
                "m.icon");
        menuRelationQueryWrapper.eq("role_id", roleId);
        menuRelationQueryWrapper.eq("m.status", ConstantCode.TRUE);
        menuRelationQueryWrapper.eq("m.delete_status", ConstantCode.TRUE);
        menuRelationQueryWrapper.eq("rm.delete_status", ConstantCode.TRUE);
        return baseMapper.getMenuByRoleId(menuRelationQueryWrapper);
    }

    @Override
    public List<TMenu> getMenuByRoleId(Set<Long> roleIds) {
        QueryWrapper<TRoleMenuRelation> menuRelationQueryWrapper = new QueryWrapper<>();
        menuRelationQueryWrapper.select("distinct m.id", "m.parent_id", "m.title", "m.level", "m.sort", "m.name",
                "m.icon");
        menuRelationQueryWrapper.in("role_id", roleIds);
        menuRelationQueryWrapper.eq("m.status", ConstantCode.TRUE);
        menuRelationQueryWrapper.eq("m.delete_status", ConstantCode.TRUE);
        menuRelationQueryWrapper.eq("rm.delete_status", ConstantCode.TRUE);
        return baseMapper.getMenuByRoleId(menuRelationQueryWrapper);
    }

    @Override
    public List<TRole> getRoleByMenuId(Long menuId) {
        QueryWrapper<TRoleMenuRelation> tRoleMenuRelationQueryWrapper = new QueryWrapper<>();
        tRoleMenuRelationQueryWrapper.select("distinct r.id", "r.name");
        tRoleMenuRelationQueryWrapper.eq("rm.menu_id", menuId);
        tRoleMenuRelationQueryWrapper.eq("r.status", ConstantCode.TRUE);
        tRoleMenuRelationQueryWrapper.eq("r.delete_status", ConstantCode.TRUE);
        tRoleMenuRelationQueryWrapper.eq("rm.delete_status", ConstantCode.TRUE);
        return baseMapper.getRoleByMenuId(tRoleMenuRelationQueryWrapper);
    }

    @Override
    public List<TRole> getRoleByMenuId(Set<Long> menuIds) {
        QueryWrapper<TRoleMenuRelation> tRoleMenuRelationQueryWrapper = new QueryWrapper<>();
        tRoleMenuRelationQueryWrapper.select("distinct r.id", "r.name");
        tRoleMenuRelationQueryWrapper.in("rm.menu_id", menuIds);
        tRoleMenuRelationQueryWrapper.eq("r.status", ConstantCode.TRUE);
        tRoleMenuRelationQueryWrapper.eq("r.delete_status", ConstantCode.TRUE);
        tRoleMenuRelationQueryWrapper.eq("rm.delete_status", ConstantCode.TRUE);
        return baseMapper.getRoleByMenuId(tRoleMenuRelationQueryWrapper);
    }
}
